<?php
session_start();
$sid=session_id();
include 'lib/phpqrcode.php';
$value = 'http://10.102.117.18/study2/pad/padlogin.php?sid='.$sid; //二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = 6;//生成图片大小
//生成二维码图片
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = '../img/index.png';//准备好的logo图片
$QR = 'qrcode.png';//已经生成的原始二维码图

if ($logo !== false) {
    $QR = imagecreatefromstring(file_get_contents($QR));
    $logo = imagecreatefromstring(file_get_contents($logo));
    $QR_width = imagesx($QR);//二维码图片宽度
    $QR_height = imagesy($QR);//二维码图片高度
    $logo_width = imagesx($logo);//logo图片宽度
    $logo_height = imagesy($logo);//logo图片高度
    $logo_qr_width = $QR_width / 5;
    $scale = $logo_width/$logo_qr_width;
    $logo_qr_height = $logo_height/$scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;
    //重新组合图片并调整大小
    imagecopyresampled(
        $QR,
        $logo,
        $from_width,
        $from_width,
        0,
        0,
        $logo_qr_width,
        $logo_qr_height,
        $logo_width,
        $logo_height
    );
}
//输出图片
imagepng($QR, 'helloweixin.png');
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>扫码登录</title>
</head>

<body>
    <img  src="helloweixin.png" alt="二维码"><br>
    <h5 >登录不便，请手机扫码操作</h5>
    <div></div>
    <script>
        var xhttp;
        if (window.XMLHttpRequest) {
          // 用于现代浏览器的代码
            xhttp = new XMLHttpRequest();
        } else {
           // 应对老版本 IE 浏览器的代码
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        };
        
        var div = document.querySelector('div');
        var timer = 60;
        var flag=0;       //此变量表示服务器上是否有sid.txt文件
        function jz(){    //60秒内如果检测到服务器上有sid.txt文件跳转到登录后的页面，否则跳转到登录页面
            if(flag==1){
                location.href = '../index.php'
            }
            else if (timer == 0) {
                location.href = '../login.php'
            } else {
                div.innerHTML = '你将在' + timer + '秒钟之后跳转到登录首页！';
                timer--;
                //flag="<?php echo  file_exists("sid.txt"); ?>";
                xhttp.onreadystatechange = function() {
                   if (this.readyState == 4 && this.status == 200) {
                      flag = this.responseText;
                   }
                };
                xhttp.open("GET", "sid.php", true);
                xhttp.send(); 
            }
        }
        setInterval(jz, 1000);
    </script>
</body>

</html>